﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace SilverlightMenu
{
    /// <summary>
    /// 用于在 Silverlight 中显示HTML的控件
    /// </summary>
    public class HTMLHost : ContentControl
    {
        int _htmlControlLeft = 0;

        public int HtmlControlLeft
        {
            get { return _htmlControlLeft; }
            set { _htmlControlLeft = value; }
        }
        int _htmlControlTop = 0;

        public int HtmlControlTop
        {
            get { return _htmlControlTop; }
            set { _htmlControlTop = value; }
        }
        readonly String _htmlControlId = System.Guid.NewGuid().ToString();

        public String HtmlControlId
        {
            get { return _htmlControlId; }
        }

        String _navigationUrl;

        public String NavigationUrl
        {
            get { return _navigationUrl; }
            set { _navigationUrl = value; }
        }

        readonly int _htmlZIndex = 0;

        public int HtmlZIndex
        {
            get { return _htmlZIndex; }
        }

        private HtmlElement divIFrameHost;

        public HTMLHost()
        {
            Loaded += new RoutedEventHandler(HtmlHost_Loaded);
        }

        void HtmlHost_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeComponent();
        }

        public void InitializeComponent()
        {
            HtmlDocument doc = HtmlPage.Document;
            divIFrameHost = doc.CreateElement("div");
            divIFrameHost.SetStyleAttribute("position", "absolute");
            divIFrameHost.SetAttribute("id", System.Guid.NewGuid().ToString());
            divIFrameHost.SetStyleAttribute("height", Height+"px");
            divIFrameHost.SetStyleAttribute("width", Width+"px");
            divIFrameHost.SetStyleAttribute("left", _htmlControlLeft.ToString() + "px");
            divIFrameHost.SetStyleAttribute("top", _htmlControlTop.ToString() + "px");
            divIFrameHost.SetStyleAttribute("z-index", _htmlZIndex.ToString());
            divIFrameHost.AppendChild(CreateIFrameControl(doc));
            doc.Body.AppendChild(divIFrameHost);
        }



     

        private HtmlElement CreateIFrameControl(HtmlDocument doc)
        {
            HtmlElement iFrame = doc.CreateElement("IFRAME");
            iFrame.SetAttribute("src", _navigationUrl);
            iFrame.SetStyleAttribute("height", "100%");
            iFrame.SetStyleAttribute("width", "100%");
            iFrame.SetStyleAttribute("left", "0px");
            iFrame.SetStyleAttribute("position", "relative");
            iFrame.SetStyleAttribute("top", "0px");
            iFrame.Id = System.Guid.NewGuid().ToString();
            return iFrame;
        }
    }
}
